/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.model;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IFileType;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.api.IWhereResult;
import net.sourceforge.perforce.core.resources.IP4File;
import net.sourceforge.perforce.ui.PerforceUIPlugin;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class P4File implements IP4File, IAdaptable {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the changelist id. */
  private Integer changelistId;
  /** Stores the action. */
  private String action;
  /** Stores the file type. */
  private IFileType fileType;
  /** Stores the depot filename. */
  private String depotFilename;
  /** Stores the revision. */
  private Integer revision;
  /** Stores the client name. */
  private String clientName;
  /** Stores the server. */
  private IPerforceServer server;
  /** Stores the 'our lock' indicator. */
  private boolean ourLock;
  /** Stores the 'resolved' indicator. */
  private boolean resolved = false;
  /** Stores the 'resolved' indicator. */
  private boolean needsResolve = false;

  //----------------------------------------------------------------------------
  // ---- Constructors

  /**
   * Constructor for P4File.
   * 
   * @param theServer the perforce server.
   */
  P4File(IPerforceServer theServer) {
    server = theServer;
  }

  // -------------------------------------------------------------------- Public
  // Methods (IP4File)

  public Integer getChangelistId() {
    return changelistId;
  }

  public String getAction() {
    return action;
  }

  public IFileType getFileType() {
    return fileType;
  }

  public String getDepotFilename() {
    return depotFilename;
  }

  public IPath getLocation() {
    try {
      IWhereResult whereResult = server.where(depotFilename);
      return new Path(whereResult.getLocalFilename());
    } catch (PerforceException e) {
      PerforceUIPlugin.log(e.getStatus());
      return Path.EMPTY;
    }
  }

  public Integer getRevision() {
    return revision;
  }

  public String getName() {
    int pos = depotFilename.lastIndexOf('/') + 1;
    return depotFilename.substring(pos);
  }

  public String getClientName() {
    return clientName;
  }

  public IPerforceServer getServer() {
    return server;
  }

  public boolean hasOurLock() {
    return ourLock;
  }

  public boolean isResolved() {
    return resolved;
  }

  public boolean needsResolve() {
    return needsResolve;
  }

  // ----------------------------------------------------------------- Public
  // Methods (IAdaptable)

  @SuppressWarnings("unchecked")
  // adapter
  public Object getAdapter(Class adapter) {
    return Platform.getAdapterManager().getAdapter(this, adapter);
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Sets the changelist id.
   * 
   * @param newChangelistId the new changelist id.
   */
  public void setChangelistId(Integer newChangelistId) {
    changelistId = newChangelistId;
  }

  /**
   * Sets the action.
   * 
   * @param newAction the new action.
   */
  public void setAction(String newAction) {
    action = newAction;
  }

  /**
   * Sets the file type.
   * 
   * @param newFileType the new file type.
   */
  public void setFileType(IFileType newFileType) {
    fileType = newFileType;
  }

  /**
   * Sets the depot filename.
   * 
   * @param newDepotFilename the new depot filename.
   */
  public void setDepotFilename(String newDepotFilename) {
    depotFilename = newDepotFilename;
  }

  /**
   * Sets the have revision.
   * 
   * @param newHaveRev the new have revision.
   */
  public void setRevision(Integer newHaveRev) {
    revision = newHaveRev;
  }

  /**
   * Sets the client name.
   * 
   * @param newClientName the new client name.
   */
  public void setClientName(String newClientName) {
    clientName = newClientName;
  }

  /**
   * Sets the our lock value.
   * 
   * @param newOurLock the lock indicator value.
   */
  public void setOurLock(boolean newOurLock) {
    ourLock = newOurLock;
  }

  /**
   * Sets the resolved value.
   * 
   * @param newResolved the new resolved value.
   */
  public void setResolved(boolean newResolved) {
    resolved = newResolved;
  }

  /**
   * Sets the needsResolved value.
   * 
   * @param newResolved the new needsResolve value.
   */
  public void setNeedsResolve(boolean newResolved) {
    needsResolve = newResolved;
  }
}
